<?php

class Activity_lottery_model extends CI_Model
{
    private $_table  = 'activity_lottery';
    private $_table2 = 'lottery_reseller';
    private $_table3 = 'lottery_info';

    /**
     * 根据用户username 查找活动
     */
    public function findLotteryByUsername($userName)
    {
        $this->db->select('lottery_id, uid, lottery_sum, winning_num, status, on_line, titles, user_name, start_date, end_date, details, adverts, winners_details_pages');
        $this->db->from($this->_table);
        $this->db->where('user_name', $userName);
        $this->db->where('status', 1); //上架
        $this->db->where('audit_state', 2); //2审核通过
        $this->db->limit(1);

        return $this->db->get();
    }

    /**
     * 根据主键查找抽奖活动
     */
    public function findById($lotteryId)
    {
        $this->db->select('activity_lottery.uid, activity_lottery.lottery_id, lottery_name, audit_state, lottery_sum, winning_num, activity_lottery.status, on_line, user_name, winners_total, user_total, created_at, start_date, end_date, page1, page2, page3, page4, page5');
        $this->db->from($this->_table.' AS activity_lottery');
        $this->db->join($this->_table3, 'activity_lottery.lottery_id = lottery_info.lottery_id', 'INNER');
        $this->db->where('activity_lottery.lottery_id', $lotteryId);
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 查找抽奖活动
     */
    public function findOne($lotteryId)
    {
        $this->db->select('activity_lottery.uid, activity_lottery.lottery_id, lottery_name, audit_state, lottery_sum, winning_num, activity_lottery.status, on_line, user_name, winners_total, user_total, created_at, start_date, end_date');
        $this->db->from($this->_table);
        $this->db->where('activity_lottery.lottery_id', $lotteryId);
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 查找活动id
     */
    public function findReseller($uid)
    {
        $this->db->select('activity_lottery.lottery_id, lottery_name');
        $this->db->from($this->_table);
        $this->db->join($this->_table2, 'activity_lottery.lottery_id = lottery_reseller.lottery_id', 'INNER');
        $this->db->where('reseller_uid', $uid);
        $this->db->where('status', 1); //上架
        $this->db->where('audit_state', 2); //2审核通过
        return $this->db->get();
    }

    /**
     * 中奖总数添加
     */
    public function setWinnersTotal($lotteryId)
    {
        $this->db->set('winners_total', "winners_total + 1", false);
        $this->db->where('lottery_id', $lotteryId);

        return $this->db->update($this->_table);
    }

    /**
     * 设置抽奖总数
     */
    public function setUserTotal($lotteryId)
    {
        $this->db->set('user_total', "user_total + 1", false);
        $this->db->where('lottery_id', $lotteryId);

        return $this->db->update($this->_table);
    }

    /**
     * 抽奖活动列表
     */
    public function selectActivityList()
    {
        $this->db->select('lottery_id, uid, winners_total, user_total, lottery_name, start_date, end_date, sort');
        $this->db->from($this->_table);
        $this->db->where('status', 1); //上架
        $this->db->where('audit_state', 2); //2审核通过
        $this->db->order_by('sort ASC');

        return $this->db->get();
    }

    public function activitySearch($keyword)
    {
        $this->db->select('lottery_id, uid, winners_total, user_total, lottery_name, start_date, end_date, sort');
        $this->db->from($this->_table);
        $this->db->where('status', 1); //上架
        $this->db->where('audit_state', 2); //2审核通过
        if ( isset($keyword) && $keyword !='') {
            $search = trim(addslashes($keyword));
            $sql = "((lottery_id like '%{$this->db->escape_like_str($keyword)}%') or (lottery_name like '%{$this->db->escape_like_str($keyword)}%'))";
            $this->db->where($sql);
        }
        $this->db->order_by('sort ASC');

        return $this->db->get();
    }
}